package net.zjitc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.zjitc.entity.Attribute;
import net.zjitc.mapper.AttributeMapper;
import net.zjitc.service.AttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AttributeServiceImpl implements AttributeService {
    @Autowired(required = false)
    private AttributeMapper attributeMapper;



    @Override
    public List<Attribute> select(Integer id,String sel) {
        QueryWrapper<Attribute> wrapper=new QueryWrapper<>();
        wrapper.eq("cat_id",id);
        wrapper.eq("attr_sel",sel);
        return attributeMapper.selectList(wrapper);
    }

    @Override
    public Attribute selectByAttrId(Integer id, Integer attrid, String attr_sel, String attr_vals) {
        QueryWrapper<Attribute> wrapper = new QueryWrapper<>();
        wrapper.eq("cat_id",id);
        wrapper.eq("attr_id",attrid);
        wrapper.eq("attr_sel",attr_sel);
        if (attr_vals!=null){
            wrapper.eq("attr_vals",attr_vals);
        }
        return attributeMapper.selectOne(wrapper);
    }

    @Override
    public Attribute addAttr(Integer id, String attr_name, String attr_sel) {
        Attribute attribute=new Attribute();
        attribute.setCat_id(id);
        attribute.setAttr_name(attr_name);
        attribute.setAttr_sel(attr_sel);
        attributeMapper.insert(attribute);
        return attribute;
    }

    @Override
    public void delete(Integer id, Integer attrid) {
        QueryWrapper<Attribute> wrapper = new QueryWrapper<>();
        wrapper.eq("cat_id",id);
        wrapper.eq("attr_id",attrid);
        attributeMapper.delete(wrapper);
    }

    @Override
    public Attribute update(Integer id, Integer attrid,Attribute attribute) {
        QueryWrapper<Attribute> wrapper=new QueryWrapper<>();
        wrapper.eq("cat_id",id);
        wrapper.eq("attr_id",attrid);
        int update = attributeMapper.update(attribute, wrapper);
        return attribute;
    }


}
